package com.pig4cloud.pig.module.controller.devices;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.devices.EstateAppointmentRecord;
import com.pig4cloud.pig.module.api.entity.estate.EstateRoom;
import com.pig4cloud.pig.module.service.devices.IEstateAppointmentRecordService;
import com.pig4cloud.pig.module.service.estate.IEstateRoomService;
import com.pig4cloud.pig.module.service.estate.IEstateService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 访客预约记录Controller
 * @author ruoyi
 * @date 2023-09-07
 */
@Validated
@Api(value = "访客预约记录控制器", tags = {"访客预约记录管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/device/robot/record")
public class EstateAppointmentRecordController {

    private final IEstateAppointmentRecordService estateAppointmentRecordService;
    private final IEstateService estateService;
    private final IPropertyService propertyService;
    private final IEstateRoomService estateRoomService;


    /**
     * 查询访客预约记录列表
     */
    @ApiOperation("查询访客预约记录列表")
    @PreAuthorize("@pms.hasPermission('module:record:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateAppointmentRecord> list(@Validated EstateAppointmentRecord estateAppointmentRecord) {
        QueryWrapper<EstateAppointmentRecord> qw = new QueryWrapper<>();

        TableDataInfo<EstateAppointmentRecord> td = PageUtils.buildDataInfo(estateAppointmentRecordService.page(PageUtils.buildPage(),qw));
        List<EstateAppointmentRecord> list = td.getData();
        for (EstateAppointmentRecord list1:list){
            if (list1.getRoomId()!=null){
                EstateRoom room = estateRoomService.findById(list1.getRoomId());
                if (room!=null){
                    list1.setRoomNo(room.getRoomNo());
                }
            }
        }
        return td;
    }

    /**
     * 导出访客预约记录列表
     */
    @ApiOperation("导出访客预约记录列表")
    @PreAuthorize("@pms.hasPermission('module:record:export')")
    @SysLog("访客预约记录")
    @GetMapping("/export")
    public List export(@Validated EstateAppointmentRecord estateAppointmentRecord) {
        List<EstateAppointmentRecord> list = estateAppointmentRecordService.list();
        // ExcelUtil<EstateAppointmentRecord> util = new // ExcelUtil<EstateAppointmentRecord>(EstateAppointmentRecord.class);
        return  list;// util.exportExcel(list, "访客预约记录");
    }

    /**
     * 获取访客预约记录详细信息
     */
    @ApiOperation("获取访客预约记录详细信息")
    @PreAuthorize("@pms.hasPermission('module:record:query')")
    @GetMapping("/{id}")
    public R<EstateAppointmentRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateAppointmentRecord estateAppointmentRecord = estateAppointmentRecordService.getById(id);
        return R.ok(estateAppointmentRecord);
    }

    /**
     * 新增访客预约记录
     */
    @ApiOperation("新增访客预约记录")
    @PreAuthorize("@pms.hasPermission('module:record:add')")
    @SysLog("访客预约记录")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody EstateAppointmentRecord estateAppointmentRecord) {
        return R.okByRow(estateAppointmentRecordService.save(estateAppointmentRecord) ? 1 : 0);
    }

    /**
     * 修改访客预约记录
     */
    @ApiOperation("修改访客预约记录")
    @PreAuthorize("@pms.hasPermission('module:record:edit')")
    @SysLog("访客预约记录")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody EstateAppointmentRecord estateAppointmentRecord) {
        return R.okByRow(estateAppointmentRecordService.updateById(estateAppointmentRecord) ? 1 : 0);
    }

    /**
     * 删除访客预约记录
     */
    @ApiOperation("删除访客预约记录")
    @PreAuthorize("@pms.hasPermission('module:record:remove')")
    @SysLog("访客预约记录")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(estateAppointmentRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
